from AStock.ASUtil import tushare_client
from AStock.ASSetting import settings
from pymongo import UpdateOne


def _main_():
    pro = tushare_client()

    # 股票基础信息
    df_stock_basic = pro.stock_basic(
        exchange='', list_status='L', fields='symbol,name,area,industry,market,list_date')

    df_stock_basic = df_stock_basic.rename(columns={'symbol': 'code'})

    coll = settings.database.stock_info_tushare
    bulk = []
    for tup in df_stock_basic.itertuples():
        item = tup._asdict()
        del item['Index']
        update_one = UpdateOne(
            {'code': tup.code},
            {'$set': item},
            upsert=True
        )
        bulk.append(update_one)
    try:
        coll.bulk_write(bulk)
        print('saved {} items to stock_info_tushare'.format(len(bulk)))
    except Exception as e:
        print(e)


if __name__ == '__main__':
    _main_()